define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    function CommissionFormat(kjtype, kjtc, wqtype, wqtc, xstype, xstc, cbtype, cbjg, bzjg) {
        const types = [
            {text: __('Payment amount ratio'), symbols: '%'},
            {text: __('Fixed amount'), symbols: '元'},
            {text: __('Monthly commission'), symbols: '元'},
        ];

        const types2 = [
            {text: __('(成交价格-成本价格)*'), symbols: '%'},
            {text: __('Fixed amount'), symbols: '元'},
            {text: __('Monthly commission'), symbols: '元'},
        ];

        const types3 = [
            {text: __('收款金额*'), symbols: '%'},
            {text: __('Fixed amount'), symbols: '元'},
            {text: __('Select cost expenses'), symbols: '元'},
        ];

        var str = '<span style="font-size: 12px">'

        if (Config['kjtc']) {
            str += __('Accounting commission') + '：' + types[kjtype].text + kjtc + types[kjtype].symbols + '<br/>'
        }
        if (Config['wqtc']) {
            str += __('Outworker commission') + '：' + types[wqtype].text + wqtc + types[wqtype].symbols + '<br/>'
        }
        if (Config['xstc']) {
            str += __('Sales commission') + '：' + types2[xstype].text + xstc + types2[xstype].symbols + '<br/>'
        }
        if (Config['cbjg']) {
            str += __('Cost price') + '：' + types3[cbtype].text + cbjg + types3[cbtype].symbols + '<br/>'
        }
        if (Config['bzjg']) {
            str += __('Standard price') + '：' + bzjg + '元<br/>'
        }
        return str
    }


    function rendCostValue(type = 'input', name, value = '') {
        var str = ''
        if (type == 'input') {
            str += '<input placeholder="' + __('Please enter') + '" id="' + name + '" data-rule="required" class="form-control" name="row[' + name + ']" value="' + value + '" type="text">'
        } else {
            const costExpenses = Config['costExpenses'] || []
            str += '<select id="c-' + name + '" value="' + value + '" class="form-control selectpicker" data-rule="required" multiple="" name="row[' + name + '][]">'
            for (var i in costExpenses) {
                str += '<option  value="' + i + '">' + costExpenses[i] + '</option>'
            }
            str += '</select>'
        }
        return str
    }

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'service/index' + location.search,
                    add_url: 'service/add',
                    edit_url: 'service/edit',
                    del_url: 'service/del',
                    multi_url: 'service/multi',
                    import_url: 'service/import',
                    table: 'service',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'service_name',
                            title: __('Name'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'ght',
                            title: __('Individual businesses'),
                            operate: false,
                            table: table,
                            class: 'autocontent',
                            formatter: function (value, row, index) {
                                return CommissionFormat(
                                    row.gth_commission_account_type,
                                    row.gth_commission_account,

                                    row.gth_commission_outworker_type,
                                    row.gth_commission_outworker,

                                    row.gth_commission_sales_type,
                                    row.gth_commission_sales,

                                    row.gth_cost_type,
                                    row.gth_cost_value,

                                    row.gth_standard_price
                                );
                            }
                        },
                        {
                            field: 'xgmnsr',
                            title: __('Small-scale Taxpayer'),
                            operate: false,
                            table: table,
                            class: 'autocontent',
                            formatter: function (value, row, index) {
                                return CommissionFormat(
                                    row.xgmnsr_commission_account_type,
                                    row.xgmnsr_commission_account,

                                    row.xgmnsr_commission_outworker_type,
                                    row.xgmnsr_commission_outworker,

                                    row.xgmnsr_commission_sales_type,
                                    row.xgmnsr_commission_sales,

                                    row.xgmnsr_cost_type,
                                    row.xgmnsr_cost_value,

                                    row.xgmnsr_standard_price
                                );
                            }
                        },
                        {
                            field: 'ybnsr',
                            title: __('General taxpayer'),
                            operate: false,
                            table: table,
                            class: 'autocontent',
                            formatter: function (value, row, index) {
                                return CommissionFormat(
                                    row.ybnsr_commission_account_type,
                                    row.ybnsr_commission_account,

                                    row.ybnsr_commission_outworker_type,
                                    row.ybnsr_commission_outworker,

                                    row.ybnsr_commission_sales_type,
                                    row.ybnsr_commission_sales,

                                    row.ybnsr_cost_type,
                                    row.ybnsr_cost_value,

                                    row.ybnsr_standard_price
                                );
                            }
                        },
                        {
                            field: 'remark',
                            width: "120px",
                            title: __('备注说明'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('查看'),
                                    title: __('查看备注说明'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: 'service/showremark',
                                    extend: 'data-area=\'["450px","300px"]\'',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                },
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {
                            field: 'create_time',
                            title: __('Create_time'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {

            $('input[name="row[gth_cost_type]"]').change((e) => {
                const value = $('input[name="row[gth_cost_type]"]:checked').val()
                if (value == 2) {
                    $('#gth_cost_value').html(rendCostValue('selete', 'gth_cost_value'))
                    Form.api.bindevent($("form[role=form]"));
                } else {
                    $('#gth_cost_value').html(rendCostValue('input', 'gth_cost_value'))
                }
            })

            $('input[name="row[xgmnsr_cost_type]"]').change((e) => {
                const value = $('input[name="row[xgmnsr_cost_type]"]:checked').val()
                if (value == 2) {
                    $('#xgmnsr_cost_value').html(rendCostValue('selete', 'xgmnsr_cost_value'))
                    Form.api.bindevent($("form[role=form]"));
                } else {
                    $('#xgmnsr_cost_value').html(rendCostValue('input', 'xgmnsr_cost_value'))
                }
            })

            $('input[name="row[ybnsr_cost_type]"]').change((e) => {
                const value = $('input[name="row[ybnsr_cost_type]"]:checked').val()
                if (value == 2) {
                    $('#ybnsr_cost_value').html(rendCostValue('selete', 'ybnsr_cost_value'))
                    Form.api.bindevent($("form[role=form]"));
                } else {
                    $('#ybnsr_cost_value').html(rendCostValue('input', 'ybnsr_cost_value'))
                }
            })


            Controller.api.bindevent();
        },
        edit: function () {

            $('input[name="row[gth_cost_type]"]').change((e) => {
                const value = $('input[name="row[gth_cost_type]"]:checked').val()
                if (value == 2) {
                    $('#gth_cost_value').html(rendCostValue('selete', 'gth_cost_value'))
                    Form.api.bindevent($("form[role=form]"));
                } else {
                    $('#gth_cost_value').html(rendCostValue('input', 'gth_cost_value'))
                }
            })

            $('input[name="row[xgmnsr_cost_type]"]').change((e) => {
                const value = $('input[name="row[xgmnsr_cost_type]"]:checked').val()
                if (value == 2) {
                    $('#xgmnsr_cost_value').html(rendCostValue('selete', 'xgmnsr_cost_value'))
                    Form.api.bindevent($("form[role=form]"));
                } else {
                    $('#xgmnsr_cost_value').html(rendCostValue('input', 'xgmnsr_cost_value'))
                }
            })

            $('input[name="row[ybnsr_cost_type]"]').change((e) => {
                const value = $('input[name="row[ybnsr_cost_type]"]:checked').val()
                if (value == 2) {
                    $('#ybnsr_cost_value').html(rendCostValue('selete', 'ybnsr_cost_value'))
                    Form.api.bindevent($("form[role=form]"));
                } else {
                    $('#ybnsr_cost_value').html(rendCostValue('input', 'ybnsr_cost_value'))
                }
            })

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
